/** <!--------------------------------------------------------------------------------------------->
 * @file XXXXXXX.XXX
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */

// -- includes --
#include "GraphBranchFrom.h"
#include <QPainter>
#include "GraphBranch.h"



GraphBranchFrom::GraphBranchFrom( GraphBranch* pFromBranch, GraphBranch* pToBranch )
    : GraphItem("MergeFrom " + pFromBranch->GetName() + " to " + pToBranch->GetName()),
      m_pFromBranch(pFromBranch), m_pToBranch(pToBranch)
{
	//
	// Need this workaround to be able to react on QGraphicsItem::ItemChildAddedChange
	//
	setParentItem(pFromBranch);
}

GraphBranchFrom::~GraphBranchFrom()
{
}


/**
 * Defines the size of the item (without it's connectors etc.) - thus the space it needs
 */
QRectF GraphBranchFrom::logicalRect() const
{
	return QRectF( S_WIDTH / -2.0f, S_HEIGHT / -2.0f, S_WIDTH, S_HEIGHT );
}


QRectF GraphBranchFrom::boundingRect() const
{
	QPointF oToPos;
	if( m_pToBranch->parentItem() ) // Map to scene coordiantes
		oToPos = m_pToBranch->parentItem()->mapToScene(m_pToBranch->pos());
	else
		oToPos = m_pToBranch->pos();	// are already scene coordinates

	QPointF oTopLeft( S_WIDTH / -2.0f, S_HEIGHT / -2.0f );
	QPointF oTopRight( mapFromScene(oToPos).x() / 2.0f, S_HEIGHT / 2.0f);
	return QRectF( oTopLeft, oTopRight );
}

void GraphBranchFrom::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(option);
	Q_UNUSED(widget);

	painter->save();

	painter->setPen( QColor(Qt::blue) );
	painter->setBrush( Qt::NoBrush );

	QPointF oToPos;
	if( m_pToBranch->parentItem() ) // Map to scene coordiantes
		oToPos = m_pToBranch->parentItem()->mapToScene(m_pToBranch->pos());
	else
		oToPos = m_pToBranch->pos();	// are already scene coordinates

	if( isEnabled() )
	{
		painter->drawLine( QPointF(S_WIDTH / 2.0f, 0), QPointF( mapFromScene(oToPos).x() / 2.0f, 0 ) );
		painter->drawArc( QRect(0, -S_HEIGHT, S_WIDTH, S_HEIGHT), 16*180, 16*90 );
	}
	else
	{
		painter->drawLine( QPointF(0, 0), QPointF( mapFromScene(oToPos).x() / 2.0f, 0 ) );
	}


	painter->restore();
}
